// 常用公共方法
// 时间格式化
import type { MenuItem, RouteItem } from '@/types/global.d'
import dayjs from 'dayjs'
export const formatTime = (val: number, style: string = 'YYYY-MM-DD HH:mm:ss') => {
  if (!val) {
    return ''
  } else {
    return dayjs(val).format(style)
  }
}

export function routesToMenus(routes: RouteItem[], base: string): MenuItem[] {
  return routes
    .filter((route) => route.meta && route.meta.title) // 只处理有 meta.title 的路由
    .map((route) => {
      const path = base ? (base + '/' + route.path) : route.path
      return {
        path,
        title: route.meta.title,
        icon: route.meta.icon,
        children:
          route.children && route.children.length > 0 ? routesToMenus(route.children, path) : []
      }
    })
}
